package com.mouliu.base.syn12;

import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;
import java.util.Iterator;
import java.util.Vector;

/**
 * 多线程使用vector或者hashTable的示例
 *
 * @author mouliu
 * @create 2018-03-30-下午9:58
 */
public class Tickets {
    public static  void main(String[] args){
        //初始化火车票池并添加火车票:避免线程同步可采用Vector替代ArrayList  HashTable替代HashMap
         final Vector<String>tickes = new Vector<>();
         for (int i=1;i<1000;i++){
             tickes.add("火车票"+i);
         }
   /*      for(Iterator iterator=tickes.iterator();iterator.hasNext();){
             String  string = (String) iterator.next();
             tickes.remove(20);
         }*/
         for (int i=1;i<=10;i++){
             new Thread("线程"+i){
                 @Override
                 public void run(){
                     while (true){
                         if (tickes.isEmpty()){
                             break;
                         }
                         System.out.println(Thread.currentThread().getName() + "---" + tickes.remove(0));
                     }

                 }
             }.start();
         }
    }
}
